<%@ Page language="vb" CodeFile="editPartner.aspx.vb" Inherits="editPartner_aspx_vb" %>

<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then
        
        subDo = Request.QueryString("subDo")
        partnerID = Request.QueryString("partnerID")
        partnerID = CLng(partnerID)
        
        dbOpen()
        RecSet = Conn.Execute("Select p.partnerName, p.Street, p.zipCode, p.city, p.pwd, p.chooseClient, p.invoiceFee, p.contractNumberPrefix, c.country From (tblPartner p INNER JOIN tblCountry c ON c.countryID = p.countryID) Where p.partnerID = " & partnerID & "")
        If RecSet.EOF Then
            Response.Redirect("searchPartner.aspx")
        Else
            partnerName = RecSet.Fields("partnerName").Value
            street = RecSet.Fields("street").Value
            zipCode = RecSet.Fields("zipCode").Value
            city = RecSet.Fields("city").Value
            pwd = RecSet.Fields("pwd").Value
            chooseClient = RecSet.Fields("chooseClient").Value
            invoiceFee = RecSet.Fields("invoiceFee").Value
            contractNumberPrefix = RecSet.Fields("contractNumberPrefix").Value
            country = RecSet.Fields("country").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select c.countryCurrency, (Select Count(d.dealerID) From tblDealer d Where d.partnerID = p.partnerID) As dealerCount, (Select Count(v.vehicleID) From tblVehicle v Where v.partnerID = p.partnerID) As vehicleCount, (Select Count(c.claimID) From (tblClaim c INNER JOIN tblVehicle v ON v.vehicleID = c.vehicleID) Where v.partnerID = p.partnerID) As claimCount, (Select Sum(c.paymentAmount) From (tblClaim c INNER JOIN tblVehicle v ON v.vehicleID = c.vehicleID) Where v.partnerID = p.partnerID) As claimSum From (tblPartner p INNER JOIN tblCountry c ON c.countryID = p.countryID) Where p.partnerID = " & partnerID & "")
        If Not RecSet.EOF Then
            countryCurrency = RecSet.Fields("countryCurrency").Value
            dealerCount = RecSet.Fields("dealerCount").Value
            vehicleCount = RecSet.Fields("vehicleCount").Value
            claimCount = RecSet.Fields("claimCount").Value
            claimSum = RecSet.Fields("claimSum").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        '*** Leasing Invoice ***
        dbOpen()
        RecSet = Conn.Execute("Select Sum(invoiceAmount) As invoiceAmount From tblInvoice Where partnerID = " & partnerID & "")
        invoiceAmount = RecSet.Fields("invoiceAmount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(invoiceAmount) Then
            invoiceAmount = 0
        End If
        invoiceSum = invoiceSum + invoiceAmount
        '*** Admin Fee Invoice ***
        dbOpen()
        RecSet = Conn.Execute("Select Sum(i.invoiceAmount) As invoiceAmount From tblInvoiceAdminFee i Where i.partnerID = " & partnerID & "")
        invoiceAmount = RecSet.Fields("invoiceAmount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(invoiceAmount) Then
            invoiceAmount = 0
        End If
        invoiceSum = invoiceSum + invoiceAmount
        '*** Credit Invoice ***
        dbOpen()
        RecSet = Conn.Execute("Select Sum(i.invoiceAmount) As invoiceAmount From tblInvoiceCredit i Where i.partnerID = " & partnerID & "")
        invoiceAmount = RecSet.Fields("invoiceAmount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(invoiceAmount) Then
            invoiceAmount = 0
        End If
        invoiceSum = CDbl(invoiceSum) - CDbl(invoiceAmount)
        '*** Distance penalty Invoice ***
        dbOpen()
        RecSet = Conn.Execute("Select Sum(i.invoiceAmount) As invoiceAmount From (tblInvoiceDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where v.partnerID = " & partnerID & "")
        invoiceAmount = RecSet.Fields("invoiceAmount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(invoiceAmount) Then
            invoiceAmount = 0
        End If
        invoiceSum = invoiceSum + invoiceAmount
        '*** Duration penalty Invoice ***
        dbOpen()
        RecSet = Conn.Execute("Select Sum(i.invoiceAmount) As invoiceAmount From (tblInvoiceDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where v.partnerID = " & partnerID & "")
        invoiceAmount = RecSet.Fields("invoiceAmount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(invoiceAmount) Then
            invoiceAmount = 0
        End If
        invoiceSum = invoiceSum + invoiceAmount
        '*** PriceList Modification Invoice ***
        dbOpen()
        RecSet = Conn.Execute("Select Sum(i.invoiceAmount) As invoiceAmount From tblInvoicePriceListModification i Where i.partnerID = " & partnerID & "")
        invoiceAmount = RecSet.Fields("invoiceAmount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(invoiceAmount) Then
            invoiceAmount = 0
        End If
        invoiceSum = invoiceSum + invoiceAmount
        If IsDBNull(invoiceSum) Then
            invoiceSum = 0
        End If
        If IsDBNull(claimSum) Then
            claimSum = 0
        End If
        Over()
        strJS = ""
        strJS = strJS + "[""partnerName"", ""==''""],"
        strJS = strJS + "[""street"", ""==''""],"
        strJS = strJS + "[""zipCode"", ""==''""],"
        strJS = strJS + "[""city"", ""==''""],"
        strJS = strJS + "[""pwd"", ""==''""],"
        strJS = strJS + "[""invoiceFee"", ""==''""],"
        strJS = strJS + "[""chooseClient"", ""<1""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>EDIT PARTNER<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='editPartner.aspx?do=save&partnerID=" & partnerID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Country:</td><td align='right'><input type='text' name='country' id='country' value='" & country & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Partner:</td><td align='right'><input type='text' name='partnerName' id='partnerName' value='" & partnerName & "' size='40' maxlength='50'></td><td><div id='error_partnerName' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Street:</td><td align='right'><input type='text' name='street' id='street' value='" & street & "' size='40' maxlength='50'></td><td><div id='error_street' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Zipcode:</td><td align='right'><input type='text' name='zipCode' id='zipCode' value='" & zipCode & "' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_zipCode' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>City:</td><td align='right'><input type='text' name='city' id='city' size='40' value='" & city & "' maxlength='50'></td><td><div id='error_city' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Partner web - password:</td><td align='right'><input type='text' name='pwd' id='pwd' size='40' value='" & pwd & "' maxlength='50'></td><td><div id='error_pwd' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Invoice fee:</td><td align='right'><input type='text' name='invoiceFee' id='invoiceFee' size='40' value='" & invoiceFee & "' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_invoiceFee' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Contract number prefix:</td><td align='right'><input type='text' name='contractNumberPrefix' id='contractNumberPrefix' size='40' value='" & contractNumberPrefix & "' maxlength='1'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Partner web - choose Lease taker:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='chooseClient' id='chooseClient'>")
        Response.Write("<option value=''> Select option  ")
        Response.Write("<option value='1'")
        If CInt(chooseClient) = 1 Then
            Response.Write(" selected='selected'")
        End If
        Response.Write("> Yes  ")
        Response.Write("<option value='2'")
        If CInt(chooseClient) = 2 Then
            Response.Write(" selected='selected'")
        End If
        Response.Write("> No  ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_chooseClient' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Save changes'></td></tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='2'>PARTNER STATISTICS<hr class='hrHeadline'></td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Number of dealers:</td><td align='right'>" & dealerCount & "</td></tr>")
        Response.Write("<tr><td>Number of lease contracts:</td><td align='right'>" & vehicleCount & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Number of claims reported:</td><td align='right'>" & claimCount & "</td></tr>")
        Response.Write("<tr><td>Total invoiced amount (" & countryCurrency & "):</td><td align='right'>" & FormatNumber(CDbl(invoiceSum), 2) & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Total claim cost (" & countryCurrency & "):</td><td align='right'>" & FormatNumber(CDbl(claimSum), 2) & "</td></tr>")
        Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then
        
        partnerID = Request.QueryString("partnerID")
        partnerName = Request.Form("partnerName")
        street = Request.Form("street")
        zipCode = Request.Form("zipCode")
        city = Request.Form("city")
        pwd = Request.Form("pwd")
        invoiceFee = Request.Form("invoiceFee")
        contractNumberPrefix = Request.Form("contractNumberPrefix")
        chooseClient = Request.Form("chooseClient")
        partnerID = CLng(partnerID)
        
        If Len(partnerName) > 0 Then
            partnerName = Trim(partnerName)
            partnerName = Replace(CStr(partnerName), "'", "")
            partnerName = Replace(CStr(partnerName), """", "")
            partnerName = Replace(CStr(partnerName), "<", "")
            partnerName = Replace(CStr(partnerName), ">", "")
            partnerName = Replace(CStr(partnerName), "=", "")
        End If
        
        If Len(street) > 0 Then
            street = Trim(street)
            street = Replace(CStr(street), "'", "")
            street = Replace(CStr(street), """", "")
            street = Replace(CStr(street), "<", "")
            street = Replace(CStr(street), ">", "")
            street = Replace(CStr(street), "=", "")
        End If
        
        If Len(zipCode) > 0 Then
            zipCode = Trim(zipCode)
            zipCode = Replace(CStr(zipCode), "'", "")
            zipCode = Replace(CStr(zipCode), """", "")
            zipCode = Replace(CStr(zipCode), "<", "")
            zipCode = Replace(CStr(zipCode), ">", "")
            zipCode = Replace(CStr(zipCode), "=", "")
        End If
        
        If Len(city) > 0 Then
            city = Trim(city)
            city = Replace(CStr(city), "'", "")
            city = Replace(CStr(city), """", "")
            city = Replace(CStr(city), "<", "")
            city = Replace(CStr(city), ">", "")
            city = Replace(CStr(city), "=", "")
        End If
        
        If Len(pwd) > 0 Then
            pwd = Trim(pwd)
            pwd = Replace(CStr(pwd), "'", "")
            pwd = Replace(CStr(pwd), """", "")
            pwd = Replace(CStr(pwd), "<", "")
            pwd = Replace(CStr(pwd), ">", "")
            pwd = Replace(CStr(pwd), "=", "")
        End If
        
        If Len(contractNumberPrefix) > 0 Then
            contractNumberPrefix = Trim(contractNumberPrefix)
            contractNumberPrefix = Replace(contractNumberPrefix, "'", "")
            contractNumberPrefix = Replace(contractNumberPrefix, """", "")
            contractNumberPrefix = Replace(contractNumberPrefix, "<", "")
            contractNumberPrefix = Replace(contractNumberPrefix, ">", "")
            contractNumberPrefix = Replace(contractNumberPrefix, "=", "")
        End If
        
        chooseClient = CLng(chooseClient)
        
        dbOpen()
        Conn.Execute("Update tblPartner Set partnerName = '" & partnerName & "', street = '" & street & "', zipCode = '" & zipCode & "', city = '" & city & "', pwd = '" & pwd & "', chooseClient = " & chooseClient & ", invoiceFee = " & invoiceFee & ", contractNumberPrefix = '" & contractNumberPrefix & "' Where partnerID = " & partnerID & "")
        dbClose()
        
        Response.Redirect("editPartner.aspx?subDo=%20-%20SAVED&partnerID=" & partnerID & "")
        
    End If
%>
